package mww.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mww.common.DaoException;
import mww.common.ServiceException;
import mww.javabean.entity.Admin;
import mww.javabean.entity.Student;
import mww.javabean.entity.Teacher;
import mww.javabean.entity.User;
import mww.javabean.service.AdminService;
import mww.javabean.service.AdminServiceImpl;
import mww.javabean.service.CourseService;
import mww.javabean.service.CourseServiceImpl;
import mww.javabean.service.StudentService;
import mww.javabean.service.StudentServiceImpl;
import mww.javabean.service.TeacherService;
import mww.javabean.service.TeacherServiceImpl;

public class LoginServlet extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AdminService adminService = new AdminServiceImpl();
		TeacherService teacherService = new TeacherServiceImpl();
		StudentService studentService = new StudentServiceImpl();
		CourseService courseService = new CourseServiceImpl();
		
		HttpSession session = request.getSession();// 获得一个Session对象，用于存放一些提示信息返回到前台
		request.setCharacterEncoding("UTF-8");// 设置请求的编码方式
		response.setCharacterEncoding("UTF-8");// 设置响应的编码方式
		// 通过request对象从前台登录页面表单获得登录名和密码数据
		String loginName = request.getParameter("loginName");
		String password = request.getParameter("password");
		String userType = request.getParameter("userType");
		try {
			User user = null;
			// 根据登录页面单选按钮的值判断用户是哪种类型用户，并将学生、教师或者管理员对象转化为User对象并设置对应的登录名、真实姓名、用户类型属性值
			if (userType!=null&&userType.equals("0")) {
				Admin admin = adminService.adminLogin(loginName, password);
				if (admin == null) {
					session.setAttribute("message", "用户名不存在！！！");
					request.getRequestDispatcher("/login.jsp").forward(request, response);
				}else{	//若存在该用户
					//再次判断密码是否吻合
					if (!admin.getPassword().equals(password)) {	//密码不吻合
						session.setAttribute("message", "密码不正确");
						request.getRequestDispatcher("/login.jsp").forward(request, response);
					} else {
					user = new User();
					user.setLoginName(loginName);
					user.setName(admin.getName());
					user.setUserType(0);
					
					int studentNumber = studentService.getStudentTotality();
					int teacherNumber = teacherService.getTeacherTotality();
					int courseNumber = courseService.getTeacherTotality();
					
					//传递学生数、教师数、课程数
					session.setAttribute("studentNumber", studentNumber);	
					session.setAttribute("teacherNumber", teacherNumber);
					session.setAttribute("courseNumber", courseNumber);
					
					// 如果登录成功将获得的User对象存在session对象中（首页需要读取session中User信息）
					session.setAttribute("user", user);
					// 如果登录成功跳转系统主页面index.jsp
					request.getRequestDispatcher("/admin/adminIndex.jsp").forward(request, response);
				}
				}

			} else if (userType!=null&&userType.equals("1")) {
				Teacher teacher = teacherService.findTeacherByTeacherNo(loginName);
				//根据登录名从数据库查找记录
				if(teacher==null){	//若不存在该用户
					session.setAttribute("message", "该教师不存在！！！");
					request.getRequestDispatcher("/login.jsp").forward(request, response);
				}else{	//若存在该用户
					//再次判断密码是否吻合
					if (!teacher.getPassword().equals(password)) {	//密码不吻合
						session.setAttribute("message", "密码不正确");
						request.getRequestDispatcher("/login.jsp").forward(request, response);
					} else {//密码吻合
						user = new User();
						user.setLoginName(teacher.getTeacherNo());
						user.setName(teacher.getTeacherName());
						user.setUserType(1);
						// 如果登录成功将获得的User对象存在session对象中（首页需要读取session中User信息）
						session.setAttribute("user", user);
						// 如果登录成功跳转系统主页面index.jsp
						request.getRequestDispatcher("/teacher/teacherIndex.jsp").forward(request, response);
					}
				}
			}else if(userType!=null&&userType.equals("2")){
				Student student = studentService.findStudentByStudentNo(loginName);
				//根据登录名从数据库查找记录
				if(student==null){	//若不存在该用户
					session.setAttribute("message", "该学生不存在！！！");
					request.getRequestDispatcher("/login.jsp").forward(request, response);
				}else{	//若存在该用户
					//再次判断密码是否吻合
					if (!student.getPassword().equals(password)) {	//密码不吻合
						session.setAttribute("message", "密码不正确");
						request.getRequestDispatcher("/login.jsp").forward(request, response);
					} else {//密码吻合
						user = new User();
						user.setLoginName(student.getStudentNo());
						user.setName(student.getStudentName());
						user.setUserType(2);
						// 如果登录成功将获得的User对象存在session对象中（首页需要读取session中User信息）
						session.setAttribute("user", user);
						// 如果登录成功跳转系统主页面index.jsp
						request.getRequestDispatcher("/student/studentIndex.jsp").forward(request, response);
					}
				}
			}			
			else {
				// 如果登录失败将登录失败提示信息放入session对象
				session.setAttribute("message", "请登录！！！");
				// 如果登录失败跳转系统登录页面login.jsp，要求用户重新登录
				request.getRequestDispatcher("/login.jsp").forward(request, response);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("se", e);
			request.getRequestDispatcher("/serviceException.jsp").forward(request, response);
		} catch (DaoException e) {
			e.printStackTrace();
			request.setAttribute("de", e);
			request.getRequestDispatcher("/daoException.jsp").forward(request, response);
		}
	}
}
